package com.aooci.ordercat.service.impl;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aooci.ordercat.dao.OrdercatDAO;
import com.aooci.ordercat.dao.OrderedItemDAO;
import com.aooci.ordercat.dao.RecetteDAO;
import com.aooci.ordercat.domain.Ordercat;
import com.aooci.ordercat.domain.Recette;
import com.aooci.ordercat.enums.OrderStatus;
import com.aooci.ordercat.service.AccountingService;
import com.aooci.ordercat.vo.RecetteSummaryVo;

@Service("accountingService")  
@Transactional
public class AccountingServiceImpl implements AccountingService, Serializable {

	private static final long serialVersionUID = -1193474493259273334L;

	private RecetteDAO recetteDAO;
	/**
	 * @param recetteDAO the recetteDAO to set
	 */
	public void setRecetteDAO(RecetteDAO recetteDAO) {
		this.recetteDAO = recetteDAO;
	}

	private OrdercatDAO ordercatDAO;
	public void setOrdercatDAO(OrdercatDAO ordercatDAO) {
		this.ordercatDAO = ordercatDAO;
	}
	
	private OrderedItemDAO orderedItemDAO;
	public void setOrderedItemDAO(OrderedItemDAO orderedItemDAO) {
		this.orderedItemDAO = orderedItemDAO;
	}

	public void init() {
		
	}


	@SuppressWarnings("deprecation")
	@Override
	public List<Ordercat> loadOrdercatsBySpecifiedDate(Date date, OrderStatus status, int divideTime) {
		if(date != null){
			Calendar myCal = new GregorianCalendar();
			myCal.setTime(date);
			myCal.add(Calendar.DATE, 1);
			Date nextDay = myCal.getTime();
			return this.ordercatDAO.selectByAcrossDayConditons(date.getYear()+1900, date.getMonth()+1, date.getDate(),nextDay.getYear()+1900, nextDay.getMonth()+1, nextDay.getDate(), status, divideTime);
		}else{
			return null;
		}
//		
//		if(date != null)
//			return this.ordercatDAO.selectByAttributes(date.getYear()+1900, date.getMonth()+1, date.getDate(), status);
//		else
//			return null;
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public List<Recette> loadRecettesBySpecifiedDate(Date date, int divideTime) {
		if(date != null){
			Calendar myCal = new GregorianCalendar();
			myCal.setTime(date);
			myCal.add(Calendar.DATE, 1);
			Date nextDay = myCal.getTime();
			return this.recetteDAO.selectByAcrossDayConditons(date.getYear()+1900, date.getMonth()+1, date.getDate(),nextDay.getYear()+1900, nextDay.getMonth()+1, nextDay.getDate(), OrderStatus.PAID, divideTime);
		}else{
			return null;
		}
	}

	@Override
	public void changeOrdercatStatus(Ordercat[] ordercats, OrderStatus newStatus) {
		for(Ordercat ordercat:ordercats){
			ordercat.setStatus(newStatus);
			this.ordercatDAO.save(ordercat);
		}
	}

	@Override
	public RecetteSummaryVo calculRecetteSummary(List<Recette> recettes) {
		RecetteSummaryVo recetteSummaryVo = new RecetteSummaryVo();
		for(Recette recette: recettes){
			recetteSummaryVo.setTotalOfCash(recetteSummaryVo.getTotalOfCash() + recette.getCash());
			recetteSummaryVo.setTotalOfCard(recetteSummaryVo.getTotalOfCard() + recette.getCard());
			recetteSummaryVo.setTotalOfCheque(recetteSummaryVo.getTotalOfCheque() + recette.getCheque());
			recetteSummaryVo.setTotalOfTVA6(recetteSummaryVo.getTotalOfTVA6() + recette.getTva6());
			recetteSummaryVo.setTotalOfTVA12(recetteSummaryVo.getTotalOfTVA12() + recette.getTva12());
			recetteSummaryVo.setTotalOfTVA21(recetteSummaryVo.getTotalOfTVA21() + recette.getTva21());
			recetteSummaryVo.setTotalOfTip(recetteSummaryVo.getTotalOfTip() + recette.getTip());
			recetteSummaryVo.setTotalOfOffert(recetteSummaryVo.getTotalOfOffert() + recette.getOffert());
			recetteSummaryVo.setTotalOfDiscount(recetteSummaryVo.getTotalOfDiscount() + recette.getDiscount());
			recetteSummaryVo.setTotalOfTotal(recetteSummaryVo.getTotalOfTotal() + recette.getOrdercat().getTotal());
			recetteSummaryVo.setTotalOfPaidTotal(recetteSummaryVo.getTotalOfPaidTotal() + recette.getFinalTotal());
		}
		
		DecimalFormat df =new DecimalFormat("#.00");
		
		recetteSummaryVo.setTotalOfDiscountAndOffert(Float.valueOf(df.format(recetteSummaryVo.getTotalOfDiscount() + recetteSummaryVo.getTotalOfOffert())));
		
		recetteSummaryVo.setBaseOfTotalTVA6(Float.valueOf(df.format(recetteSummaryVo.getTotalOfTVA6()/1.06)));
		recetteSummaryVo.setBaseOfTotalTVA12(Float.valueOf(df.format(recetteSummaryVo.getTotalOfTVA12()/1.12)));
		recetteSummaryVo.setBaseOfTotalTVA21(Float.valueOf(df.format(recetteSummaryVo.getTotalOfTVA21()/1.21)));
		recetteSummaryVo.setTotalOfBase(Float.valueOf(df.format(recetteSummaryVo.getBaseOfTotalTVA6() + recetteSummaryVo.getBaseOfTotalTVA12() + recetteSummaryVo.getBaseOfTotalTVA21())));
		
		recetteSummaryVo.setmOfTVA6(Float.valueOf(df.format(recetteSummaryVo.getTotalOfTVA6() - recetteSummaryVo.getBaseOfTotalTVA6())));
		recetteSummaryVo.setmOfTVA12(Float.valueOf(df.format(recetteSummaryVo.getTotalOfTVA12() - recetteSummaryVo.getBaseOfTotalTVA12())));
		recetteSummaryVo.setmOfTVA21(Float.valueOf(df.format(recetteSummaryVo.getTotalOfTVA21() - recetteSummaryVo.getBaseOfTotalTVA21())));
		recetteSummaryVo.setTotalOfM(Float.valueOf(df.format(recetteSummaryVo.getmOfTVA6() + recetteSummaryVo.getmOfTVA12() + recetteSummaryVo.getmOfTVA21())));
		
		return recetteSummaryVo;
	}

	@Override
	public void deleteOrdercats(Ordercat[] ordercats) {
		for(Ordercat ordercat : ordercats){
			this.recetteDAO.deleteByOrdercatId(ordercat.getId());
			this.orderedItemDAO.deleteByOrdercatId(ordercat.getId());
			this.ordercatDAO.delete(ordercat);
		}
	}
	

}
